package com.sixteengroup.personnelms.controller.portal;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageInfo;
import com.sixteengroup.personnelms.bean.Menu;
import com.sixteengroup.personnelms.bean.User;
import com.sixteengroup.personnelms.service.admin.LogService;
import com.sixteengroup.personnelms.service.personnel.GetUserInfo;
import com.sixteengroup.personnelms.service.portal.UserService;
import com.sixteengroup.personnelms.util.DateUtil;
import com.sixteengroup.personnelms.util.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    LogService logService;

    @Autowired
    GetUserInfo userInfo;

    /*
     * 前端用json格式向后端传递数据，后端以对象形式接收，并在参数前加@RequestBody注解
     * */
    @RequestMapping("/login")
    public Map<String, Object> login(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userService.login(user);
        int type = 0;
        if (user1 != null){
            type = user1.getType();
        }
        try {
            if (user1 != null && type!=-2) {
                if (type == 2){  //外部用户登录时获取状态
                    map.put("userStatus", userInfo.getUserStatus(user1.getId()));
                }
                map.put("code", 200);
                map.put("msg", "登录成功");
                map.put("user", user1);
                map.put("token", JWTutil.getToken(user1));
                logService.saveLoginLog(user1);  //保存登录日志
            } else {
                map.put("code", 201);
                if (type==-2){
                    map.put("msg", "账号被禁用！");
                }else {
                    map.put("msg", "登录失败！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }

    /*
    注册时
    异步验证账号是否存在
    */
    @RequestMapping("/accountExist/{account}")
    public Map<String, Object> accountExist(@PathVariable("account") String account) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userService.isExist(account);
        if (user1 == null) {    //账号不存在
            map.put("code", 200);
            map.put("msg", "√");
        } else {
            map.put("code", 201);
            map.put("msg", "账号已存在");
        }
        return map;
    }

    /*
    修改密码时
    异步验证密码是否正确
    */
    @RequestMapping("/getPass/{account}")
    public Map<String, Object> getPass(@PathVariable("account") String account) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userService.isExist(account);
        map.put("oldPass", user1.getPassword());
        return map;
    }

    /*
   修改密码
   */
    @RequestMapping("/changePass")
    public Map<String, Object> changePass(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userService.updatePass(user);
        try {
            map.put("code", 200);
            map.put("msg", "修改成功");

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }

    @RequestMapping("/reg")
    public Map<String, Object> register(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userService.register(user);
        try {
            map.put("code", 200);
            map.put("msg", "注册成功");

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }

    //动态获取用户菜单
    @RequestMapping("/findMenuList")
    public Map<String, Object> findMenuList(@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        //从token中获取用户信息
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();
        int type = tokenInfo.getClaim("type").asInt();
        List<Menu> list = userService.findMenuList(id, type);
        try {
            map.put("code", 200);
            map.put("menuList", list);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }

    //获取外部用户账号列表
    @RequestMapping("/getAccList")
    public Map<String,Object> getAccList(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        PageInfo<User> pageInfo = userService.getAccList(user);
        try {
            map.put("code",200);
            map.put("List",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","服务器错误！");
        }
        return map;
    }

     /*
     * 修改账号状态*/
    @RequestMapping("/updateType")
    public Map<String, Object> updateType(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        userService.updateType(user);
        try {
            map.put("code", 200);
            map.put("msg", "修改成功");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }

    /*
     * 获取进度
     * */
    @RequestMapping("/updatestatus")
    public Map<String, Object> updatestatus(@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        DecodedJWT tokenInfo = JWTutil.getTokenInfo(token);
        int id = tokenInfo.getClaim("id").asInt();
        int userStatus = userInfo.getUserStatus(id);
        String dn = userInfo.getDeptName(id);
        try {
            map.put("userStatus",userStatus);
            map.put("deptName",dn);
            map.put("date", DateUtil.dateToString(new Date(),"yyyy-MM-dd"));
            map.put("code", 200);
            map.put("msg", "修改成功");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "服务器错误！");
        }
        return map;
    }
}
